package service

import (
	"fmt"
	"github.com/Baidu-AIP/golang-sdk/aip/censor"
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat-moments_0926/config"
	"wechat-moments_0926/internal/model"
	"wechat-moments_0926/internal/params"
)

func AddComment(c *gin.Context) {
	var req params.AddCommentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	//文本审核
	yn := config.Conf.BaiDuZhiNenYn

	client := censor.NewClient(yn.API, yn.Secret)
	res := client.TextCensor(req.Content)
	fmt.Println(res)

	var comment model.Comment

	comment = model.Comment{
		FeedId:  req.FeedId,
		UserId:  req.UserId,
		Content: req.Content,
	}

	err := comment.AddComment()

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "评论失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "评论成功",
		"data":    comment.Content,
	})
}
